#define _CRT_SECURE_NO_WARNINGS 1


  struct ListNode {
      int val;
      ListNode *next;
      ListNode(int x) : val(x), next(nullptr) {}
  };
class Solution {
public:
    ListNode* getIntersectionNode(ListNode* headA, ListNode* headB)
    {
        if (!headA || !headB) return nullptr;
        ListNode* cur1 = headA, * cur2 = headB;
        while (cur1 || cur2)
        {
            if (cur1 == cur2)
            {
                return cur1;
            }
            if (!cur1)
            {
                cur1 = headB;
            }
            else
            {
                //out<<"cur1="<<cur1->val<<endl;
                cur1 = cur1->next;
            }
            if (!cur2)
            {
                cur2 = headA;
            }
            else
            {
                //cout<<"cur2="<<cur2->val<<endl;
                cur2 = cur2->next;
            }
        }
        return nullptr;
    }
};